const db = wx.cloud.database();
// pages/goods_detail.js
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		goodsObject: {},
		isShoucang: false
	},
	goodsInfomation: {},
	

	/**
	 * 生命周期函数--监听页面加载
	 */
	
	onLoad: function(options) {
		this.goods_id = options.goods_id;
		this.getGoodsDetail();
		this.showShouCang();
	},
	
	async getGoodsDetail() {
		const goodsObject = await db.collection('goods_index')
			.where({
				"message.goods_id": Number(this.goods_id)
			})
			.get()
			.then( res => {
				console.log(res);
				let Result = res.data[0].message;
				this.goodsInfomation = Result;
				
				this.setData({
					goodsObject: {
						goods_NavImg: Result.goods_NavImg,
						goods_name: Result.goods_name,
						goods_price: Result.goods_price,
						goods_richText: Result.goods_richText,
						goods_Img: Result.goods_Img
					}
				})
			})
	},

	//收藏
	async handleGoodsShoucang(){
		let Cloudres = '';
		const userInfo = wx.getStorageSync('userInfo');
		console.log(userInfo);
		const res = await db.collection("goods_detail_shoucang")
		.where({
			'goods_id': this.goods_id
		})
		.get()
		.then((result) => {
			Cloudres = result.data[0];
		})
		
		if(!userInfo){
			wx.showToast({
				title:'请先前往 我的 进行登录',
				icon:'none',
				mask:true
			})
		return;
		}else if(userInfo && !Cloudres){
			this.setData({
				isShoucang: true
			})
			await db.collection("goods_detail_shoucang")
			.add({
				data: {
					isShoucang: this.data.isShoucang,
					goods_id: this.goods_id,
					...this.data.goodsObject
				}
			});
		return;
		}else if(userInfo && Cloudres){
			console.log("有res");
			await db.collection("goods_detail_shoucang")
			.where({
				'goods_id': this.goods_id
			})
			.remove({
				success:(res) => {
					this.setData({
						isShoucang: false
					})
				}
			})
		return;
		}

	},


	//展示收藏
	async showShouCang(){
		const userInfo = wx.getStorageSync('userInfo');
		if(!userInfo){
			return;
		}else{
			const res = await db.collection("goods_detail_shoucang")
			.where({
				'goods_id': this.goods_id
			})
			.get()
			.then((result) => {
				if(result.data[0]){
					let {isShoucang} = this.data;
					isShoucang = result.data[0].isShoucang;
					this.setData({
						isShoucang
					})
				}
			});
		}
		
	},
	
	handleCarAdd() {
		let cart = wx.getStorageSync("cart_detail")||[];
		let index = cart.findIndex(v => v.goods_id === this.goodsInfomation.goods_id);
		if(index === -1){
			this.goodsInfomation.num = 1;
			this.goodsInfomation.Choose = true;
			cart.push(this.goodsInfomation);
		}else{
			cart[index].num++;
		};
		wx.setStorageSync("cart_detail",cart);
		wx.showToast({
			title:'添加到购物车',
			icon:'success',
			mask:'true'
			
		})
	},
	
	BuyNow(){
		const address = wx.getStorageSync("address");
		const userInfo = wx.getStorageSync("userInfo");
		const goods_id = this.goods_id;
		if(address && userInfo){
			wx.navigateTo({
				url:'/pages/PayNow/PayNow',
				success:(res) => {
					res.eventChannel.emit('sendGoodsId', { goods_id })
				}
			})
		}else if(!userInfo){
			wx.showToast({
				title:'请先前往 我的 登录',
				icon:'none',
				mask:true
			})
		}else if(!address){
			wx.showToast({
				title:'请先前往 购物车 添加收货地址',
				icon:'none',
				mask:true
			})
		}

	}
})
